<?php
/**
 * Bureaucracy Plugin: Creates/validates forms
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-08-01
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_bureaucracy2 extends DokuWiki_Syntax_Plugin {

    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Andreas Gohr, Stephane Chamberland',
            'email'  => 'andi@splitbrain.org',
            'date'   => '2009-08-01',
            'name'   => 'Bureaucracy2 Plugin',
            'desc'   => 'A simple form generator/validator',
            'url'    => 'http://dokuwiki.org/plugin:bureaucracy2',
        );
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * What about paragraphs?
     */
    function getPType(){
        return 'block';
    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 155;
    }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('<form>.*?</form>',$mode,'plugin_bureaucracy2');
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        $match = substr($match,6,-7); // remove form wrap

        $buro =& plugin_load('helper', 'bureaucracy2');
        if (!is_null($buro)) {
            return $buro->_parse_form_def($match);
        } else {
            $elements = array();
            $actions = array();
            return array('elements'=>$elements,'actions'=>$actions,'thanks'=>'','name'=>'','show'=>'');
        }
    }

    /**
     * Create output
     */
    function render($format, &$R, $data) {
        global $ID;
        if($format != 'xhtml') return false;
        $R->info['cache'] = false; // don't cache

        $buro =& plugin_load('helper', 'bureaucracy2');
        if (is_null($buro) || plugin_isdisabled('bureaucracy2')) return false;

        $R->doc .= $buro->_renderHTML($data);

        return true;
    }

}

//Setup VIM: ex: et ts=4 enc=utf-8 :
